//
//  ViewController.m
//  TableViewCell的删除
//
//  Created by dllo on 16/3/28.
//  Copyright © 2016年 付冬梅. All rights reserved.
//

#import "ViewController.h"
#import "TableViewContact.h"
#import "TableViewCellOfContact.h"
#import "SecondViewController.h"


@interface ViewController ()<UITableViewDataSource, UITableViewDelegate, deleteContact, deleteDelegate>

@property (nonatomic, retain) TableViewContact *tableView;
@property (nonatomic, retain) NSMutableDictionary *bigDic;
@property (nonatomic, retain) NSArray *allKeys;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.tableView = [[TableViewContact alloc] initWithFrame:CGRectMake(0, 50, self.view.frame.size.width, self.view.frame.size.height - 50) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[TableViewCellOfContact class] forCellReuseIdentifier:@"TableViewCellOfContact"];
    NSString *path = [[NSBundle mainBundle] pathForResource:@"DLS160101" ofType:@"plist"];
    self.bigDic = [NSMutableDictionary dictionaryWithContentsOfFile:path];
    self.allKeys = [[self.bigDic allKeys] sortedArrayUsingSelector:@selector(compare:)];
    UIButton *editButton = [UIButton buttonWithType:UIButtonTypeCustom];
    editButton.frame = CGRectMake(0, 0, 50, 50);
    editButton.backgroundColor = [UIColor greenColor];
    [editButton setTitle:@"下一页" forState:UIControlStateNormal];
    [editButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [self.view addSubview:editButton];
    [editButton addTarget:self action:@selector(nextButtonAction:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)nextButtonAction:(UIButton *)next {
    SecondViewController *secVc = [[SecondViewController alloc] init];
    [self presentViewController:secVc animated:YES completion:^{
        
    }];
}

//- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
//    [super setEditing:editing animated:animated];
//    [self.tableView setEditing:editing animated:animated];
//}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.allKeys.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSString *key = self.allKeys[section];
    NSArray *arr = [self.bigDic objectForKey:key];
    return arr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TableViewCellOfContact *cell = [tableView dequeueReusableCellWithIdentifier:@"TableViewCellOfContact"];
    NSString *key = self.allKeys[indexPath.section];
    NSArray *arr = [self.bigDic objectForKey:key];
    
    cell.nameLabel.text = arr[indexPath.row][@"name"];
    cell.phoneLabel.text = arr[indexPath.row][@"phoneNumber"];
    cell.buttonDelete = self;
    cell.clickButtonDeleteContact = self;
    return cell;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
//    TableViewCellOfContact *cell = (TableViewCellOfContact *)[tableView cellForRowAtIndexPath:indexPath];
    ((TableViewCellOfContact *)cell).bigView.contentOffset = CGPointMake(0, 0);
}

- (void)clickButtonName:(NSString *)name phone:(NSString *)phone {
//    NSString *str = [[name substringToIndex:1] uppercaseString];
//    NSLog(@"%@", str);
//    NSMutableArray *arr = [self.bigDic objectForKey:str];
//    NSMutableArray *arr1 = [NSMutableArray array];
//    for (NSMutableDictionary *dic in arr) {
//        if ([dic[@"name"] isEqualToString:name]) {
//            
////            [arr removeLastObject];
//        }
//        
//    }
//    self.allKeys = [[self.bigDic allKeys] sortedArrayUsingSelector:@selector(compare:)];
//    NSLog(@"%@", arr);
//    [self.tableView reloadData];
}

- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
    //在滑动手势删除某一行时显示更多按钮
    /** 添加一个删除按钮 */
    UITableViewRowAction *deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        NSLog(@"删除");
        //更新数据
        
        //更新UI
//        NSIndexPath *firstIndexPath =
        
    }];
    return @[deleteRowAction];
}

- (void)clickButtonDeleteContact:(TableViewCellOfContact *)contact {
//    NSIndexPath *indexPath = [self.tableView indexPathForCell:contact];
//    NSLog(@"%@", indexPath);
//    NSString *key = self.allKeys[indexPath.section];
//    NSMutableArray *arr = [self.bigDic objectForKey:key];
//    [arr removeObjectAtIndex:indexPath.row];
//    
//    [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
//    NSLog(@"%@", arr);
////    self.tableView deleteSections: withRowAnimation:<#(UITableViewRowAnimation)#>
//    if ([arr  isEqual: @[]]) {
//        [self.bigDic removeObjectForKey:key];
//    }
//    self.allKeys = [[self.bigDic allKeys] sortedArrayUsingSelector:@selector(compare:)];
//    [self.tableView reloadData];
    
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return self.allKeys[section];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
